package com.data.modules.online.cgreport.service.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import com.data.modules.online.cgreport.common.CgReportConstant;
import com.data.modules.online.cgreport.service.CgReportExcelServiceI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("cgReportExcelService")
public class CgReportExcelServiceImpl implements CgReportExcelServiceI {

    private static final Logger f383a = LoggerFactory.getLogger(CgReportExcelServiceImpl.class);

    @Override
    public HSSFWorkbook exportExcel(String title, Collection<?> titleSet, Collection<?> dataSet) {
        HSSFWorkbook hSSFWorkbook = null;
        if (titleSet != null) {
            try {
                if (titleSet.size() != 0) {
                    if (title == null) {
                        title = "";
                    }
                    HSSFWorkbook hSSFWorkbook2 = new HSSFWorkbook();
                    try {
                        HSSFSheet createSheet = hSSFWorkbook2.createSheet(title);
                        Row createRow = createSheet.createRow(0);
                        createRow.setHeight((short) 450);
                        HSSFCellStyle a = createCellStyle(hSSFWorkbook2);
                        List<Map> list = (List) titleSet;
                        Iterator<?> it = dataSet.iterator();
                        int i = 0;
                        for (Map map : list) {
                            Cell createCell = createRow.createCell(i);
                            createCell.setCellValue(new HSSFRichTextString((String) map.get("field_txt")));
                            createCell.setCellStyle(a);
                            i++;
                        }
                        HSSFCellStyle c = createCellStyleTwo(hSSFWorkbook2);
                        int i2 = 0;
                        while (it.hasNext()) {
                            int i3 = i2 + 1;
                            Row createRow2 = createSheet.createRow(i3);
                            Map map2 = (Map) it.next();
                            int i4 = 0;
                            for (Map map3 : list) {
                                String str = (String) map3.get(CgReportConstant.f368o);
                                String obj = map2.get(str) == null ? "" : map2.get(str).toString();
                                Cell createCell2 = createRow2.createCell(i4);
                                HSSFRichTextString hSSFRichTextString = new HSSFRichTextString(obj);
                                createCell2.setCellStyle(c);
                                createCell2.setCellValue(hSSFRichTextString);
                                i4++;
                            }
                            i2 = i3;
                        }
                        for (int i5 = 0; i5 < list.size(); i5++) {
                            createSheet.autoSizeColumn(i5);
                        }
                        return hSSFWorkbook2;
                    } catch (Exception e2) {
                        hSSFWorkbook = hSSFWorkbook2;
                        f383a.error(e2.getMessage(), e2);
                        return hSSFWorkbook;
                    }
                }
            } catch (Exception e3) {
                f383a.error(e3.getMessage(), e3);
                return hSSFWorkbook;
            }
        }

        return hSSFWorkbook;
    }

    private HSSFCellStyle createCellStyle(HSSFWorkbook hSSFWorkbook) {
        HSSFCellStyle createCellStyle = hSSFWorkbook.createCellStyle();
        createCellStyle.setBorderLeft(BorderStyle.THIN);
        createCellStyle.setBorderRight(BorderStyle.THIN);
        createCellStyle.setBorderBottom(BorderStyle.THIN);
        createCellStyle.setBorderTop(BorderStyle.THIN);
        createCellStyle.setAlignment(HorizontalAlignment.CENTER);
        createCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        createCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return createCellStyle;
    }

    private void m333a(int i, int i2, HSSFWorkbook hSSFWorkbook) {
        HSSFSheet sheetAt = hSSFWorkbook.getSheetAt(0);
        HSSFCellStyle c = createCellStyleTwo(hSSFWorkbook);
        for (int i3 = 1; i3 <= i; i3++) {
            Row createRow = sheetAt.createRow(i3);
            for (int i4 = 0; i4 < i2; i4++) {
                createRow.createCell(i4).setCellStyle(c);
            }
        }
    }

    private HSSFCellStyle m334b(HSSFWorkbook hSSFWorkbook) {
        HSSFCellStyle createCellStyle = hSSFWorkbook.createCellStyle();
        createCellStyle.setBorderLeft(BorderStyle.THIN);
        createCellStyle.setBorderRight(BorderStyle.THIN);
        createCellStyle.setBorderBottom(BorderStyle.THIN);
        createCellStyle.setBorderTop(BorderStyle.THIN);
        createCellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.index);
        createCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return createCellStyle;
    }

    private HSSFCellStyle createCellStyleTwo(HSSFWorkbook hSSFWorkbook) {
        HSSFCellStyle createCellStyle = hSSFWorkbook.createCellStyle();
        createCellStyle.setBorderLeft(BorderStyle.THIN);
        createCellStyle.setBorderRight(BorderStyle.THIN);
        createCellStyle.setBorderBottom(BorderStyle.THIN);
        createCellStyle.setBorderTop(BorderStyle.THIN);
        return createCellStyle;
    }
}
